<?php 
	namespace Home\Controller;
	use Think\Controller;

	class SysUserController extends AuthController
	{
		public function index(){return;}

		public function all()
		{
			if(IS_POST){
				// 根据当前用户的id查出来显示的用户信息
				$userid = session("user_id");
				// dump($userid);exit;
				$province_name = I("post.province_name","");
				// dump($province_name);exit();
				$output = D("SysUser")->get_canlook_user($userid,$province_name);
				$output = array_values($output);
				echo json_encode($output);
			}else{
				$userid = session("user_id");
				$province_name = I("post.province","安徽");
				$output = D("SysUser")->get_canlook_user($userid,$province_name);
				echo json_encode($output);
			}		
		}

		public function get_canuse_role()
		{
			if(IS_POST){
				// 根据当前用户的id查出来显示的用户信息
				$userid = session("user_id");
				$output = D("SysUser")->get_canuse_role($userid);
				echo json_encode($output);
			}else{
				$userid = session("user_id");
				$output = D("SysUser")->get_canuse_role($userid);
				echo json_encode($output);
			}
		}

		public function get_canuse_province()
		{
			if(IS_POST){
				// 根据当前用户的id查出来显示的用户信息
				$userid = session("user_id");
				$output = D("SysUser")->get_canuse_province($userid);
				echo json_encode($output);
			}else{
				$userid = session("user_id");
				$output = D("SysUser")->get_canuse_province($userid);
				echo json_encode($output);
			}
		}

		public function add()
		{
			if(IS_POST){
				$username = I("post.username","");
				$password = md5(I("post.password","admin"));
				$status = I("post.status","");
				$province_name = I("post.province","");
				$role_name = I("post.role","");
				$res = D("SysUser")->add_user($username,$password,$status,$province_name,$role_name);
				$res?$output = 1:$output = 0;
				echo json_encode($output);
			}else{
				// $username = I("post.username","");
				// $password = md5(I("post.password","admin"));
				// $status = I("post.status","");
				// $province_name = I("post.province","安徽,辽宁");
				// $role_name = I("post.role","普通用户");
				// $res = D("SysUser")->add_user($username,$password,$status,$province_name,$role_name);
				// $res?$output = 1:$output = 0;
				// echo json_encode($output);
			}
		}

		public function delete() 
		{    //只能删除比自己低级别和同省的用户
			if(IS_POST){
				// 根据当前用户的id查出来显示的用户信息
				$userid = session("user_id");
				$del_userid = I("post.id","");
				$code = D("SysUser")->delete_user($userid,$del_userid);
				if($code == 2){
					$code = 2;
				}elseif($code == 1){
					$code = 1;
				}else{
					$code = 0;
				}
				echo json_encode($code);
			}else{
				$userid = session("user_id");
				$del_userid = I("post.id","");
				$code = D("SysUser")->delete_user($userid,$del_userid);
				// dump($code);exit();
				if($code == 2){
					$code = 2;
				}elseif($code == 1){
					$code = 1;
				}else{
					$code = 0;
				}
				echo json_encode($code);
			}
		}

		public function edit_select()
		{
			if(IS_POST){
				$userid = session("user_id");
				$edit_userid = I("post.id","");
				$res = D("SysUser")->edit_select($userid,$edit_userid);
				if($res){
					if($res == 2){
						$output = 2;
					}
					$output = $res;
				}else{
					$output = "";
				}
				echo json_encode($output);
			}else{
				$userid = session("user_id");
				$edit_userid = I("post.id","4");
				$res = D("SysUser")->edit_select($userid,$edit_userid);
				if($res){
					if($res == 2){
						$output = 2;
					}
					$output = $res;
				}else{
					$output = "";
				}
				// dump($output);exit;
				echo json_encode($output);
			}
		}

		public function edit()
		{
			if(IS_POST){
				// 获取到修改的用户信息
				$param['id'] = I("post.id","");
				$param['username'] = I("post.username","");
				$param['status'] = I("post.status","");
				$param['province_name'] = I("post.province","");
				$param['role_name'] = I("post.role","");
				$res =  D("SysUser")->edit($param);
				$res?$output = 1:$output = 0;
				echo json_encode($output);
			}else{
				// 获取到修改的用户信息
				$param['id'] = I("post.id","35");
				$param['username'] = I("post.username","qqnn2");
				$param['status'] = I("post.status","");
				$param['province_name'] = I("post.province_name","安徽,辽宁,浙江");
				$param['role_name'] = I("post.role_name","普通用户");
				$res = $param['edit_userid'] = D("SysUser")->edit($param);
				$res?$output = 1:$output = 0;
				echo json_encode($output);
			}
		}

		// 修改密码
		public function modify_pass()
		{		
			if(IS_POST){
				// 当前用户id
				$userid = session("user_id");
				$oldpassword = I("post.oldpass","");
				$newpassword = I("post.newpass","");
				$output = D("SysUser")->modify_pass($userid,$oldpassword,$newpassword);
				// dump($output);exit;
				echo json_encode($output);
			}else{
				$userid = I("post.id","31");
				$oldpassword = I("post.oldpassword","admin");
				$newpassword = I("post.newpassword","123");
				$output = D("SysUser")->modify_pass($userid,$oldpassword,$newpassword);
				// dump($output);exit;
				echo json_encode($output);
			}
		}

		public function get_cate()
	    {
	    	$role_id = session("role_id");
	    	$per = A("Auth")->getPermiss($role_id);
			if($per[0]){
				// $output['result'] = $this->getState("0"); 
				echo json_encode($per);
			}else{
				$output['result'] = $this->getState("6"); 
				echo json_encode($output);
			}	
	    }


	}
 ?>